<?php

class BlogController extends Zend_Controller_Action {

    public function indexAction() {
        $this->_forward('all');
    }

    public function allAction() {
        $tableEntries = new Application_Model_DbTable_Entries();
        $db = Zend_Db_Table::getDefaultAdapter();
        $adapter = new Zend_Paginator_Adapter_DbSelect(
                        $db->select()
                        ->from(array('e' => 'entries'), array('id', 'title', 'date', 'author', 'body'))
                        ->join(array('u' => 'users'),
                                'e.user_id = u.id', array())
                        ->where('u.disabled = ?', false)
                        ->order('e.date desc')
        );
        $paginator = new Zend_Paginator($adapter);
        $page = $this->_getParam('page', 1);
        $itemCountPerPage = 3;
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;

        $this->view->headTitle('zendblog')->headTitle('all blogs')->setSeparator(' :: ');
        $this->view->title = 'all blogs';
    }

    public function authorAction() {
        if (!$this->_hasParam('user') || $this->view->isValidUser($this->_getParam('user'))) {
            $this->_forward('error', 'error', 'default');
        }

        $tableEntries = new Application_Model_DbTable_Entries();
        $result = $tableEntries->select()
                        ->from($tableEntries)
                        ->where('author=?', $this->_getParam('user'))
                        ->order('date desc');

        $adapter = new Zend_Paginator_Adapter_DbSelect($result);
        $paginator = new Zend_Paginator($adapter);
        $page = $this->_getParam('page', 1);
        $itemCountPerPage = 3;
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;

        $db = Zend_Db_Table::getDefaultAdapter();
        $title = $db->fetchOne(
                                $db->select()
                                ->from(array('b' => 'blogs'), array('title'))
                                ->join(array('u' => 'users'), 'b.user_id = u.id')
                                ->where('u.disabled = ?', false)
                                ->where('u.username = ?', $this->_getParam('user'))
        );

        $this->view->headTitle('zendblog')->headTitle($title)->setSeparator(' :: ');
        $this->view->title = $title;
    }

    public function entryAction() {
        if ($this->view->isValidUser($this->_getParam('user'))) {
            $this->_forward('error', 'error', 'default');
        }
        $form = new Application_Form_Comment();

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $form->removeElement('name');
            $form->removeElement('url');
            $form->addElement('hidden', 'name', array(
                'value' => Zend_Auth::getInstance()->getIdentity()->username,
                'required' => true
            ));
            $form->addElement('hidden', 'url', array(
                'value' => $this->view->baseurl() . '/' . Zend_Auth::getInstance()->getIdentity()->username,
                'required' => true
            ));
        }

        $values = array(
            'id' => $this->_getParam('id'),
            'note' => ZendBlog_Filter_HtmlComment::getLabel()
        );

        if (!$this->getRequest()->isPost()) {
            $form->populate($values);
        } elseif (!$form->isValid($_POST)) {
            $this->view->isInvalid = true;
            $form->populate($values);
        } else {
            $table = new Application_Model_DbTable_Comments();

            $data = array(
                'name' => $form->getValue('name'),
                'url' => $form->getValue('url'),
                'entry_id' => $this->_getParam('id'),
                'comment' => $form->getValue('comment')
            );

            $table->insert($data);
            $values = array_merge($values, array(
                        'name' => '',
                        'url' => '',
                        'comment' => '',
                    ));
            $form->populate($values);
        }
        $tableEntries = new Application_Model_DbTable_Entries();
        $resultEntries = $tableEntries->find($this->_getParam('id'));
        $tableComments = new Application_Model_DbTable_Comments();
        $resultComments = $tableComments->fetchAll(
                                $tableComments->select()
                                ->where('entry_id = ?', $this->_getParam('id'))
        );

        if (count($resultEntries) == 0) {
            // ID invalid so forward user to Index
            $this->_forward('index', 'index');
        }

        $adapter = new Zend_Paginator_Adapter_DbSelect(
                                $tableComments->select()
                                ->where('entry_id = ?', $this->_getParam('id'))
                                ->order('date desc')
        );
        $paginator = new Zend_Paginator($adapter);
        $page = $this->_getParam('page', 1);
        $itemCountPerPage = 2;
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
        $this->view->commentForm = $form;
        $this->view->entry = $resultEntries->current();

        $db = Zend_Db_Table::getDefaultAdapter();
        $title = $db->fetchOne(
                                $db->select()
                                ->from(array('b' => 'blogs'), array('title'))
                                ->join(array('u' => 'users'), 'b.user_id = u.id')
                                ->where('u.disabled = ?', false)
                                ->where('u.username = ?', $this->_getParam('user'))
        );

        $this->view->headTitle('zendblog')->headTitle($title)->headTitle($title)
                ->setSeparator(' :: ');
        $this->view->title = $title;
    }

}

